#include "Pool.h"

myTetris::Pool::Pool(const int& pool_height, const int& pool_width, const int& pool_margin, const std::string& background_path, const std::string& wall_path)
{
	int margin = ((pool_margin > myTetris::MAX_BLOCK_SIZE || pool_margin <= 0) ? myTetris::MAX_BLOCK_SIZE : pool_margin);
	this->pool_margin = margin;
	this->pool_height = pool_height;
	if (this->pool_height <= 0)this->pool_height = margin;
	else if (this->pool_height > myTetris::MAX_POOL_HEIGHT)this->pool_height = myTetris::MAX_POOL_HEIGHT;
	this->pool_width = pool_width;
	if (this->pool_width <= 0)this->pool_width = margin;
	else if (this->pool_width > myTetris::MAX_POOL_WIDTH)this->pool_width = myTetris::MAX_POOL_WIDTH;
	int M = pool_height + margin;
	if (M < 2 * margin)
	{
		M = 2 * margin;
		this->pool_height = margin;
	}
	int N = pool_width + 2 * margin;
	if (N < 3 * margin)
	{
		N = 3 * margin;
		this->pool_width = margin;
	}
	this->pool_shape.resize(M, std::vector<int>(N, 0));
	for (int i = 0; i < this->pool_height; i++)
	{
		for (int j = 0; j < margin; j++)
		{
			this->pool_shape[i][j] = myTetris::WALL_CODE;
			this->pool_shape[i][N - 1 - j] = myTetris::WALL_CODE;
		}
	}
	for (int i = this->pool_height; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			this->pool_shape[i][j] = myTetris::WALL_CODE;
		}
	}
	std::string cur_background_root_path = background_path;
	if (cur_background_root_path == "")
	{
		cur_background_root_path = ".\\Images\\Init\\Pool\\Background\\0.png";
	}
	else
	{
		if (cur_background_root_path.size() < 25 || cur_background_root_path.substr(0, 25) != ".\\Images\\Pool\\Background\\")
		{
			cur_background_root_path = ".\\Images\\Pool\\Background\\" + cur_background_root_path;
		}
		if (cur_background_root_path.substr(cur_background_root_path.size() - 4, 4) != ".png")
		{
			cur_background_root_path += ".png";
		}
	}
	loadimage(&this->background, cur_background_root_path.c_str(), myTetris::SINGAL_BLOCK_LENGTH * this->pool_width, myTetris::SINGAL_BLOCK_LENGTH * this->pool_height);
	std::string cur_wall_root_path = wall_path;
	if (cur_wall_root_path == "")
	{
		cur_wall_root_path = ".\\Images\\Init\\Pool\\Wall\\0.png";
	}
	else
	{
		if (cur_wall_root_path.size() < 19 || cur_wall_root_path.substr(0, 19) != ".\\Images\\Pool\\Wall\\")
		{
			cur_wall_root_path = ".\\Images\\Pool\\Wall\\" + cur_wall_root_path;
		}
		if (cur_wall_root_path.substr(cur_wall_root_path.size() - 4, 4) != ".png")
		{
			cur_wall_root_path += ".png";
		}
	}
	loadimage(&this->wall, cur_wall_root_path.c_str(), myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH);
}

int myTetris::Pool::getPoolWidth() const
{
	return this->pool_width;
}

int myTetris::Pool::getPoolHeight() const
{
	return this->pool_height;
}

int myTetris::Pool::getPoolMargin() const
{
	return this->pool_margin;
}

std::vector<std::vector<int>> myTetris::Pool::getPoolShape() const
{
	return this->pool_shape;
}

void myTetris::Pool::landBlockInPool(const int& i, const int& j, const int& skin)
{
	this->pool_shape[i][j + this->pool_margin] = skin;
}

void myTetris::Pool::overlapLineWithNewLine(const int& i, const int& j)
{
	if (i<0 || i>this->pool_height || j<0 || j>this->pool_height)return;
	this->pool_shape[i] = this->pool_shape[j];
}

void myTetris::Pool::draw(const int& x, const int& y)
{
	putimage(x, y, &this->background);
	for (int i = 0; i < this->pool_height; i++)
	{
		putimage(x - myTetris::SINGAL_BLOCK_LENGTH, y + i * myTetris::SINGAL_BLOCK_LENGTH, &this->wall);
		putimage(x + this->pool_width * myTetris::SINGAL_BLOCK_LENGTH, y + i * myTetris::SINGAL_BLOCK_LENGTH, &this->wall);
	}
	for (int i = -1; i <= this->pool_width; i++)
	{
		putimage(x + i * myTetris::SINGAL_BLOCK_LENGTH, y + this->pool_height * myTetris::SINGAL_BLOCK_LENGTH, &this->wall);
	}
}
